//
//  VCOfDiscoverList.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/1.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfDiscoverList.h"
#import "XYMacro.h"

@interface VCOfDiscoverList () <TopPageBarDelegate, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *myScrollView;

@property (nonatomic, strong) BaseTableView *contentViewOne;
@property (nonatomic, strong) BaseTableView *contentViewTwo;
@property (nonatomic, strong) UICollectionView *contentViewThree;

@property(nonatomic, strong) UIView *customHeaderView;
@property (nonatomic, strong) UIView *customView;
@property (nonatomic, strong) UIImageView *picBackImageView;
@property (nonatomic, strong) UIImageView *picImageView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) DiscoverTopPageBar *typeBar;
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *subscriptionButton;
@property (nonatomic, strong) UIButton *socialButton;
@property (nonatomic, strong) UIView *headerViewOne;
@property (nonatomic, strong) UIView *headerViewTwo;
@property (nonatomic, strong) UIView *headerViewThree;
@property (nonatomic, assign) BOOL isSecond;
@property (nonatomic, assign) BOOL isSelf;
@property (nonatomic, strong) DiscoverListModelFirst *firstModel;
@property (nonatomic, strong) DiscoverListModelSecond *secondModel;

@property (nonatomic, strong) UILabel *proLabel;
@property (nonatomic, strong) NSString *proStr;

@property (nonatomic, strong) PlayVCModelOfCollect *dingyueModel;
@property (nonatomic, strong) NSUserDefaults *user;
@property (nonatomic, strong) NSString *uid;
@property (nonatomic, assign) NSInteger count;
@property (nonatomic, strong) NSMutableArray *modelArr;



@end

@implementation VCOfDiscoverList

- (void)viewDidLoad {
    [super viewDidLoad];
    _modelArr = [NSMutableArray array];
    [self handleData];
    [self configNavigationController];
    [self configViewController];
    self.isSecond = NO;
    self.isSelf = NO;
}


- (void)handleData {
    
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22item_id%22%3A%22";
    NSString *str2 = @"%22%7D";
    
    NSString *body = [[str1 stringByAppendingString:_item_id] stringByAppendingString:str2];
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/item.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _firstModel = [DiscoverListModelFirst modelWithDictionary:result];
        //赋值
        [_picImageView sd_setImageWithURL:[NSURL URLWithString:_firstModel.data.item_img] placeholderImage:PLACEHOLDERIMAGE];
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:_firstModel.data.item_managerimg] placeholderImage:PLACEHOLDERIMAGE];
        _nameLabel.text = _firstModel.data.item_manager;
        _numberLabel.text = [NSString stringWithFormat:@"订阅数(%ld)", _firstModel.data.item_watchcnt];
        _titleLabel.text = _firstModel.data.item_desc;
        self.title = _firstModel.data.item_name;
        
        [_contentViewOne reloadData];
        [_contentViewTwo reloadData];
        
    } andFailureBlock:^(NSError *error) {}];
    
    NSString *str3 = @"json=%7B%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A1%7D%2C%22orderBy%22%3A2%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22recomm%22%3A0%2C%22item_id%22%3A%22";
    NSString *str4 = @"%22%2C%22keyword%22%3A%22%22%7D";
    NSString *body1 = [[str3 stringByAppendingString:_item_id] stringByAppendingString:str4];
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:body1 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _secondModel  = [DiscoverListModelSecond modelWithDictionary:result];
        _modelArr = (NSMutableArray *)_secondModel.data;
        if ([_secondModel.paginated.more isEqualToString:@"1"]) {
            [self setupUpRefresh];
        }
        [_contentViewOne reloadData];
        [_contentViewTwo reloadData];
    } andFailureBlock:^(NSError *error) {}];
}


#pragma mark --------- 订阅功能---------------
- (void)sendDingyueMessage {
    
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%22";
    NSString *str2 = @"%22%2C%22sid%22%3A%22d7e3db20ebb641caa6fee8bab4e33f07%22%7D%2C%22operate%22%3A1%2C%22item_ids%22%3A%22";
    NSString *str3 = @"%22%7D";
    NSString *body = [[[[str1 stringByAppendingString:_uid] stringByAppendingString:str2] stringByAppendingString:[NSString stringWithFormat:@"%ld", _firstModel.data.item_id]] stringByAppendingString:str3];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/procwatch.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        
        _dingyueModel = [PlayVCModelOfCollect modelWithDictionary:result];
        
        if (_dingyueModel.status.succeed == 1 && _dingyueModel.proclist.length != 0) {
            _proStr = @"订阅成功";
            [self createPromptBox];
        }
        if (_dingyueModel.status.succeed == 1 && _dingyueModel.proclist.length == 0) {
            _proStr = @"已经订阅";
            [self createPromptBox];
        }
        
    } andFailureBlock:^(NSError *error) {}];
    

    
}



- (void)configNavigationController {
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

- (void)configViewController {
    
    self.view.backgroundColor = BACKGROUNDCOLOR;
    [self createScrollView];
    
}

- (void)createScrollView {
    
    _myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, _myScrollView.frame.size.height);
    [self createContentViewOne];
    [self createContentViewTwo];
    _myScrollView.pagingEnabled = YES;
    _myScrollView.bounces = NO;
    _myScrollView.delegate = self;
    _myScrollView.showsHorizontalScrollIndicator = NO;
    _myScrollView.scrollEnabled = NO;
    [self.view addSubview:_myScrollView];
    
}


- (void)createContentViewOne {
    _contentViewOne = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _contentViewOne.backgroundColor = BACKGROUNDCOLOR;
    _headerViewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    [self createTableVewHeaderView];
    [_headerViewOne addSubview:_customHeaderView];
    _contentViewOne.tableHeaderView = _headerViewOne;

    _contentViewOne.delegate = self;
    _contentViewOne.dataSource = self;
    _contentViewOne.bounces = NO;
    [_myScrollView addSubview:_contentViewOne];
    [_contentViewOne registerClass:[DiscoverTableViewCellFifth class] forCellReuseIdentifier:@"DiscoverTableViewCellFifth"];
}

- (void)createContentViewTwo {
    _contentViewTwo = [[BaseTableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 1, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _contentViewTwo.backgroundColor = BACKGROUNDCOLOR;
    _headerViewTwo = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    _contentViewTwo.tableHeaderView = _headerViewTwo;
    _contentViewTwo.delegate = self;
    _contentViewTwo.dataSource = self;
    _contentViewTwo.bounces = NO;
    [_myScrollView addSubview:_contentViewTwo];
    //[_contentViewTwo registerClass:[DiscoverTableViewCellSixth class] forCellReuseIdentifier:@"DiscoverTableViewCellSixth"];
    
}


- (void)createTableVewHeaderView {
    
    
    _customHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
    _customHeaderView.backgroundColor = [UIColor colorWithRed:0.08 green:0.09 blue:0.12 alpha:1.00];
    
    
    _picBackImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pro_img_bg"]];
    [_customHeaderView addSubview:_picBackImageView];
       [_picBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_customHeaderView.mas_top).with.offset(10);
        make.left.equalTo(_customHeaderView.mas_left).with.offset(20);
        make.width.mas_offset(75);
        make.height.mas_offset(68);
    }];
    
    _picImageView = [[UIImageView alloc] init];
    _picBackImageView.userInteractionEnabled = YES;
    _picImageView.userInteractionEnabled = YES;
    [_picBackImageView addSubview:_picImageView];
    
    //创建手势
    UITapGestureRecognizer *picTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPageOfIntroduction:)];

    [_picImageView addGestureRecognizer:picTap];
    [_picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_picBackImageView.mas_top).with.offset(0);
        make.left.equalTo(_picBackImageView.mas_left).with.offset(0);
        make.bottom.equalTo(_picBackImageView.mas_bottom).with.offset(-3);
        make.right.equalTo(_picBackImageView.mas_right).with.offset(-12);
        
    }];
    
    _customView = [[UIView alloc] init];
    _customView.backgroundColor = [UIColor colorWithRed:0.16 green:0.16 blue:0.22 alpha:1.00];
    [_customHeaderView addSubview:_customView];
    [_customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_customHeaderView.mas_bottom).with.offset(0);
        make.left.equalTo(_customHeaderView.mas_left).with.offset(0);
        make.right.equalTo(_customHeaderView.mas_right).with.offset(0);
        make.height.mas_offset(80);
    }];
    
    UIView *lineOne = [[UIView alloc] init];
    lineOne.backgroundColor = [UIColor colorWithRed:0.40 green:0.40 blue:0.43 alpha:1.00];
    [_customView addSubview:lineOne];
    [lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_customView.mas_top).with.offset(0);
        make.left.equalTo(_customView.mas_left).with.offset(0);
        make.right.equalTo(_customView.mas_right).with.offset(0);
        make.height.mas_offset(1);
    }];
    
    _typeBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 1, SCREEN_WIDTH, 40) withTitleArr:@[@"全部节目", @"批量下载"]];
    _typeBar.delegate = self;
    [_customView addSubview:_typeBar];
    
    UIView *lineTwo = [[UIView alloc] init];
    lineOne.backgroundColor = [UIColor colorWithRed:0.40 green:0.40 blue:0.43 alpha:1.00];
    [_customView addSubview:lineTwo];
    [lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_customView.mas_top).with.offset(40);
        make.left.equalTo(_customView.mas_left).with.offset(0);
        make.right.equalTo(_customView.mas_right).with.offset(0);
        make.height.mas_offset(1);
    }];
    
    UIView *lineThree = [[UIView alloc] init];
    lineThree.backgroundColor = [UIColor colorWithRed:0.98 green:0.19 blue:0.00 alpha:1.00];
    [_customHeaderView addSubview:lineThree];
    [lineThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_customHeaderView.mas_left).with.offset(20);
        make.right.equalTo(_customHeaderView.mas_right).with.offset(-20);
        make.top.equalTo(_picImageView.mas_bottom).with.offset(30);
        make.height.mas_offset(1);
    }];
    
    _numberLabel = [[UILabel alloc] init];
    _numberLabel.textColor = [UIColor colorWithRed:0.39 green:0.40 blue:0.43 alpha:1.00];
    _numberLabel.font = [UIFont systemFontOfSize:12];
    [_customHeaderView addSubview:_numberLabel];
    [_numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_picImageView.mas_bottom).with.offset(0);
        make.left.equalTo(_customHeaderView.mas_left).with.offset(20);
        make.bottom.equalTo(lineThree.mas_top).with.offset(0);
        make.width.mas_offset(100);
    }];
    
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.clipsToBounds = YES;
    _headerImageView.userInteractionEnabled = YES;
    
    //创建手势
    UITapGestureRecognizer *headerPicTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPageOfAnchor:)];

    [_headerImageView addGestureRecognizer:headerPicTap];
    [_customHeaderView addSubview:_headerImageView];
    [_headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_customHeaderView.mas_top).with.offset(15);
        make.left.equalTo(_picImageView.mas_right).with.offset(15);
        make.width.mas_offset(35);
        make.height.mas_offset(35);
    }];
    [_headerImageView layoutIfNeeded];
    _headerImageView.layer.cornerRadius = 17.5;
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.textColor = [UIColor redColor];
    _nameLabel.font = [UIFont systemFontOfSize:14];
    [_customHeaderView addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headerImageView.mas_right).with.offset(10);
        make.height.mas_offset(20);
        make.width.mas_offset(100);
        make.centerY.equalTo(_headerImageView.mas_centerY);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont systemFontOfSize:15];
    [_customHeaderView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_picImageView.mas_right).with.offset(10);
        make.right.equalTo(_customHeaderView.mas_right).with.offset(20);
        make.top.equalTo(_headerImageView.mas_bottom).with.offset(5);
        make.height.mas_offset(30);
    }];
    
    _subscriptionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_subscriptionButton setImage:[UIImage imageNamed:@"album_ding1-1"] forState:UIControlStateNormal];
    //订阅
    [_subscriptionButton addTarget:self action:@selector(clickDingyueButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [_customHeaderView addSubview:_subscriptionButton];
    [_subscriptionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineThree.mas_bottom).with.offset(15);
        make.bottom.equalTo(_customView.mas_top).with.offset(-15);
        make.left.equalTo(_customHeaderView.mas_left).with.offset(20);
        make.width.mas_offset(80);
        
    }];
    
    _socialButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_socialButton setImage:[UIImage imageNamed:@"shequ"] forState:UIControlStateNormal];
    [_customHeaderView addSubview:_socialButton];
    [_socialButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineThree.mas_bottom).with.offset(15);
        make.bottom.equalTo(_customView.mas_top).with.offset(-15);
        make.right.equalTo(_customHeaderView.mas_right).with.offset(-20);
        make.width.mas_offset(80);
        
    }];
}

//订阅
- (void)clickDingyueButton:(UIButton *)button {
    
    if (![_user objectForKey:@"password"]) {
        _proStr = @"尚未登录,请先登录";
        [self createPromptBox];
    } else {
        
        [self sendDingyueMessage];
        
    }
}





- (void)goBack:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    if ([[self.navigationController.viewControllers lastObject] isKindOfClass:[VCOfDiscover class]]) {
        self.tabBarController.tabBar.hidden = NO;
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
        
    } else if ([[self.navigationController.viewControllers lastObject] isKindOfClass:[VCOfLoveListen class]]) {
        self.tabBarController.tabBar.hidden = NO;
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
    } else {
        self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    }
}

//手势的点击方法
- (void)nextPageOfIntroduction:(UIImageView *)imageView {
    VCOfIntroduction *nextIntroduction = [[VCOfIntroduction alloc] init];
    
    nextIntroduction.model = _firstModel;
    [self.navigationController pushViewController:nextIntroduction animated:YES];
    
}

- (void)nextPageOfAnchor:(UIImageView *)imageView {
    VCOfAnchor *nextAnchor = [[VCOfAnchor alloc] init];
    nextAnchor.item_managerid = [NSString stringWithFormat:@"%ld", _firstModel.data.item_managerid];
    [self.navigationController pushViewController:nextAnchor animated:YES];
}

    

#pragma mark -------- tableView 的协议--------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_firstModel.status.succeed == 1 && _secondModel.status.succeed == 1) {
        //return _secondModel.data.count;
        
        return _modelArr.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _contentViewOne) {
        DiscoverTableViewCellFifth *cell = [tableView dequeueReusableCellWithIdentifier:@"DiscoverTableViewCellFifth" forIndexPath:indexPath];
        DiscoverListModelSecondData *model =  [_modelArr objectAtIndex:indexPath.row];//[_secondModel.data objectAtIndex:indexPath.row];
        cell.model = model;
        return cell;
    } else {
        DiscoverTableViewCellSixth *cell = [tableView cellForRowAtIndexPath:indexPath];
        if (cell == nil) {
            cell = [[DiscoverTableViewCellSixth alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DiscoverTableViewCellSixth"];
            DiscoverListModelSecondData *model = [_secondModel.data objectAtIndex:indexPath.row];
            cell.model = model;

        }
        
        return cell;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor yellowColor];
    return cell;
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _myScrollView) {
        
        _customHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        [self.view addSubview:_customHeaderView];
    }

    if (scrollView == _contentViewOne && _isSecond == NO) {
        if (scrollView.contentOffset.y >= 160.f) {
            [self.view addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view.mas_top).with.offset(0);
                make.left.equalTo(self.view.mas_left).with.offset(0);
                make.right.equalTo(self.view.mas_right).with.offset(0);
            }];
            
        }
        if (scrollView.contentOffset.y < 160.f) {
            [_customHeaderView addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_customHeaderView.mas_bottom).with.offset(0);
                make.left.equalTo(_customHeaderView.mas_left).with.offset(0);
                make.right.equalTo(_customHeaderView.mas_right).with.offset(0);
                make.height.mas_offset(80);
            }];
        }
    }
    
    if (scrollView == _contentViewTwo && _isSecond == YES) {
        if (scrollView.contentOffset.y >= 160.f) {
            [self.view addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.view.mas_top).with.offset(0);
                make.left.equalTo(self.view.mas_left).with.offset(0);
                make.right.equalTo(self.view.mas_right).with.offset(0);
            }];
            
        }
        if (scrollView.contentOffset.y < 160.f) {
            [_customHeaderView addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_customHeaderView.mas_bottom).with.offset(0);
                make.left.equalTo(_customHeaderView.mas_left).with.offset(0);
                make.right.equalTo(_customHeaderView.mas_right).with.offset(0);
                make.height.mas_offset(80);
            }];
        }

    }
}


- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
    
    _myScrollView.contentOffset = CGPointMake(indexPath.item *SCREEN_WIDTH, 0);
    if (indexPath.item == 0 && _isSelf == YES) {
        if (_customView.superview == self.view) {
            [_customHeaderView addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_customHeaderView.mas_bottom).with.offset(0);
                make.left.equalTo(_customHeaderView.mas_left).with.offset(0);
                make.right.equalTo(_customHeaderView.mas_right).with.offset(0);
                make.height.mas_offset(80);
            }];
        }
        
        _customHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        [_headerViewOne addSubview:_customHeaderView];
        _contentViewOne.contentOffset = CGPointMake(0, 0);
        _isSecond = NO;
        _isSelf = NO;
        }
    if (indexPath.item == 1 && _isSelf == NO) {
        _customHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 240);
        [_headerViewTwo addSubview:_customHeaderView];
        if (_customView.superview == self.view) {
            [_customHeaderView addSubview:_customView];
            [_customView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(_customHeaderView.mas_bottom).with.offset(0);
                make.left.equalTo(_customHeaderView.mas_left).with.offset(0);
                make.right.equalTo(_customHeaderView.mas_right).with.offset(0);
                make.height.mas_offset(80);
            }];
        }
        _contentViewTwo.contentOffset = CGPointMake(0, 0);
        _isSecond  = YES;
        _isSelf = YES;
    }
}



- (void)createKVO {
    //KVO
    [_myScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 2);
    // 修改Redline位置.
    self.typeBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_typeBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_typeBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (tableView == _contentViewOne) {
        VCOfPlay *nextVC = [VCOfPlay sharePlayVC];
        
        DiscoverListModelSecondData *model = [_secondModel.data objectAtIndex:indexPath.row];
        
        nextVC.mp3URL = model.music_media;
        nextVC.index = indexPath.row;
        
        NSMutableArray *mArr = [NSMutableArray array];
        for (DiscoverListModelSecondData *model in _modelArr) {
            NSString *str = model.music_media;
            [mArr addObject:str];
        }
        nextVC.mp3URLArr = mArr;
        
        nextVC.imagePicURL = model.music_img;
        nextVC.titleStr = model.music_title;
        NSString *content_id = [NSString stringWithFormat:@"%ld", model.music_id];
        nextVC.content_id = content_id;
                
        [self.navigationController pushViewController:nextVC animated:YES];
    }
}


#pragma mark -------- 加载----------
- (void)setupUpRefresh {
    self.contentViewOne.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreBills)];
}


- (void)loadMoreBills {
    _count += 1;
    NSString *str1 = @"json=%7B%22pagination%22%3A%7B%22count%22%3A25%2C%22page%22%3A";
    NSString *str2 = @"%7D%2C%22orderBy%22%3A2%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22recomm%22%3A0%2C%22item_id%22%3A%22";
    NSString *str3 = @"%22%2C%22keyword%22%3A%22%22%7D";
    NSString *body = [[[[str1 stringByAppendingString:[NSString stringWithFormat:@"%ld", _count]] stringByAppendingString:str2] stringByAppendingString:_item_id] stringByAppendingString:str3];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/musiclist.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        DiscoverListModelSecond *model = [DiscoverListModelSecond modelWithDictionary:result];
        if ([model.paginated.more isEqualToString:@"1"]) {
            [_modelArr addObjectsFromArray:model.data];
            [self.contentViewOne reloadData];
            [self.contentViewOne.mj_footer endRefreshing];
        } else {
            [self.contentViewOne.mj_footer endRefreshingWithNoMoreData];
        }

    } andFailureBlock:^(NSError *error) {}];


    
}



#pragma mark -----------创建自定义提示栏-----------------
- (void)createPromptBox {
    UIWindow *windows = [UIApplication sharedApplication].keyWindow;
    UIView *customPromptBox = [[UIView alloc]init];
    customPromptBox.backgroundColor = BACKGROUNDCOLOR;
    customPromptBox.frame = CGRectMake(0, 0, 200, 50);
    customPromptBox.center = windows.center;
    customPromptBox.clipsToBounds = YES;
    customPromptBox.layer.cornerRadius = 5;
    customPromptBox.layer.borderWidth = 2;
    customPromptBox.layer.borderColor = [UIColor redColor].CGColor;
    
    _proLabel = [[UILabel alloc] init];
    _proLabel.textAlignment = NSTextAlignmentCenter;
    _proLabel.backgroundColor = BACKGROUNDCOLOR;
    _proLabel.textColor = [UIColor whiteColor];
    _proLabel.text = _proStr;
    _proLabel.frame = CGRectMake(10, 0, customPromptBox.frame.size.width - 20, 50);
    [customPromptBox addSubview:_proLabel];
    [windows addSubview:customPromptBox];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
        //2秒以后移除view
        [customPromptBox removeFromSuperview];
    });
}


- (void)viewWillDisappear:(BOOL)animated {
    [_myScrollView removeObserver:self forKeyPath:@"contentOffset"];
}


- (void)viewDidAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    [self createKVO];
}

- (void)viewWillAppear:(BOOL)animated {
    
    _user = [NSUserDefaults standardUserDefaults];
    
    if ([_user objectForKey:@"password"]) {
        _uid = [_user objectForKey:@"target_id"];
    }

}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
